package com.terren.spider.handler.impl;


import java.util.HashSet;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.terren.spider.core.html.configuration.Configuration;
import com.terren.spider.core.html.scheme.ICustomScheme;
import com.terren.spider.core.html.util.ConfigurationUtil;
import com.terren.spider.core.html.util.HtmlFetchUtil;
import com.terren.spider.core.html.util.HtmlParseUtil;
import com.terren.spider.core.html.util.HtmlSaveUtil;
import com.terren.spider.entity.core.Entry;
import com.terren.spider.handler.ISpiderHandler;
import com.terren.spider.util.common.BeanUtil;


public class HtmlSpiderHandlerImpl<T> implements ISpiderHandler <T> {

	private HtmlFetchUtil<T> htmlFetchUtil;// 页面抓取工具类
	private HtmlParseUtil<T> htmlParseUtil;// 页面分析工具类
	private HtmlSaveUtil htmlSaveUtil;
	private Configuration configuration;
	protected Logger log = LoggerFactory.getLogger(getClass());

	public HtmlSpiderHandlerImpl(Entry entry,Class<T> clazz) throws Exception {
		init(entry, clazz);
	}
	
	/**
	 * 抓取目标页链接
	 */
	public String[]  findUrl(String...urls) throws Exception {
		return htmlFetchUtil.findUrl(urls);
	}
	
	/**
	 * 抓取目标页链接
	 */
	public String[]  findUrl() throws Exception {
		return htmlFetchUtil.findUrl();
	}
	
	/**
	 * 抓取并解析目标页
	 */
	public int parsePage(String... urls) throws Exception {
		//do ----
		Set<String> targetUrls = new HashSet<String>();
		int pageSize  = 0;
		/**
		 * 自定义 目标页 方案
		 */
		for (ICustomScheme	scheme : configuration.getSchemes()) {
			if (BeanUtil.includeMethod("findTargetUrl", scheme.getClass())) {
				Set<String> temp = scheme.findTargetUrl(configuration);
				log.info("[entry:"+configuration.getEntry().getEntryId()+"]"+"[scheme:"+scheme.getClass().getName());
				log.info("[method:findTargetUrl]"+"[size:"+temp.size()+"]");
				if (null!=temp) {
					targetUrls.addAll(temp);
				}
			}
		}
		if (CollectionUtils.isNotEmpty(targetUrls)) {
			//String[] customUrls = targetUrls.toArray(new String[targetUrls.size()]);
			String[] customUrls = BeanUtil.set2Array(targetUrls);
			pageSize = pageSize + htmlFetchUtil.parsePage(customUrls);
		}
		
		pageSize = pageSize + htmlFetchUtil.parsePage(urls);
		
		/**
		 * 自定义 后期处理
		 */
		for (ICustomScheme	scheme : configuration.getSchemes()) {
			if (BeanUtil.includeMethod("postFetch", scheme.getClass())) {
				scheme.postFetch(configuration);
			}
		}
		return pageSize;
	}
	

	/**
	 * 初始化参数配置
	 */
	public void initConfig(Entry entry) {
		this.configuration = new ConfigurationUtil(entry).getConfiguration();
	}

	public void init(Entry entry,Class<T> clazz) throws Exception {
		initConfig(entry);
		htmlFetchUtil = new HtmlFetchUtil<T>(this.configuration,clazz);
		htmlParseUtil = new HtmlParseUtil<T>(this.configuration);
		htmlSaveUtil = new HtmlSaveUtil();
		htmlFetchUtil.getDbCollectorPipeline().setParseUtil(htmlParseUtil);
		htmlFetchUtil.getDbCollectorPipeline().setSaveUtil(htmlSaveUtil);
		htmlFetchUtil.getConsolePipeline().setParseUtil(htmlParseUtil);
	}
	

	public HtmlFetchUtil<T> getHtmlFetchUtil() {
		return htmlFetchUtil;
	}

	public void setHtmlFetchUtil(HtmlFetchUtil<T> htmlFetchUtil) {
		this.htmlFetchUtil = htmlFetchUtil;
	}

	public HtmlParseUtil<T> getHtmlParseUtil() {
		return htmlParseUtil;
	}

	public void setHtmlParseUtil(HtmlParseUtil<T> htmlParseUtil) {
		this.htmlParseUtil = htmlParseUtil;
	}


	public Configuration getConfiguration() {
		return configuration;
	}

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
		if (null != this.getHtmlFetchUtil()) {
			this.getHtmlFetchUtil().setConfiguration(configuration);
		}
	}

	public HtmlSaveUtil getHtmlSaveUtil() {
		return htmlSaveUtil;
	}

	public void setHtmlSaveUtil(HtmlSaveUtil htmlSaveUtil) {
		this.htmlSaveUtil = htmlSaveUtil;
	}
	public String getCollectItemsJson() {
		return htmlSaveUtil != null ? htmlSaveUtil.getWebTestJson():"[]";
	}
}
